/**
 * Created by Fly on 2016/9/18.
 */
var DBPool=require('../util/DBpool');
module.exports= {
    add:function (req,res,callback){
        var petsex=req.body.sex;
        var varietieno=req.body.varietie;
        var steril=req.body.steril;
        var petbirth=req.body.year+'-'+req.body.month+'-'+req.body.day;
        var weight=req.body.weight;
        var sketch=req.body.sketch;
        var usertel=req.session.usertel;
        var sql="INSERT INTO t_petinfo(petsex,varietieno,steril,petbirth,weight,sketch,userno)VALUES(?,(SELECT varietieno FROM t_varietie WHERE varietiename=?),?,?,?,?,?)";
        DBPool.getConnection(function (client) {
            client.query('SELECT userno FROM t_userinfo WHERE tel=?',[usertel],function (error,result) {
                if(error){
                    console.log(error.message);
                    return;
                }
                var userno=result[0].userno;
                client.query(sql,[petsex,varietieno,steril,petbirth,weight,sketch,userno],function (error,result) {
                    if(error){
                        console.log(error.message);
                        return;
                    }
                    callback(result);
                    client.release();
                });
            });
        });
    },
    getinfo:function (req,res,callback){
        var usertel=req.session.usertel;
        var sql="SELECT a.petno,a.sketch,a.varietieno,b.varietiename FROM t_varietie b,t_petinfo a WHERE a.userno=? AND a.varietieno=b.varietieno order by a.petno desc";
        DBPool.getConnection(function (client) {
            client.query('SELECT userno FROM t_userinfo WHERE tel=?',[usertel],function (error,result) {
                if(error){
                    console.log(error.message);
                    return;
                }
                var userno=result[0].userno;
                client.query(sql,[userno],function (error,result) {
                    if(error){
                        console.log(error.message);
                        return;
                    }
                    callback(result);
                    client.release();
                });
            });
        });
    },
    del:function (petno,callback){
        var sql="DELETE FROM t_petinfo WHERE petno=?";
        DBPool.getConnection(function (client) {
            client.query(sql,[petno],function (error,result) {
                if(error){
                    console.log(error.message);
                    return;
                }
               callback(result);
                client.release();
            });
        });
    }
};
